/**
 * @file	drv_can.h
 * @brief   MCU驱动-CAN驱动
 * @author  Liu wei
 * @version 1.0.1
 * @date    2023-04-17
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-17 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

#ifndef __DRV_CAN_H_
#define __DRV_CAN_H_

/* File Includes -------------------------------------------------------------*/
#include "mcu_type.h"
/* Cplusplus -----------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

/* Macro/Define/Typedef ------------------------------------------------------*/
/** @brief CAN 通道*/
enum
{
	DRV_CAN0 ,
	DRV_CAN1 , 
	DRV_CAN_MAX , 
};
/** @brief 帧类型 */
#define CAN_FRAME_DATA     (CAN_DATA_FRAME)
#define CAN_FRAME_REMOTE   (CAN_REMOTE_FRAME)

/** @brief ID类型 */
#define CAN_ID_STD         (CAN_STD_ID)
#define CAN_ID_EXT         (CAN_EXT_ID)

/** @brief CAN邮箱地址 */
#define RX_MB_ID00         (0)
#define RX_MB_ID01         (1)
#define RX_MB_ID02         (2)
#define RX_MB_ID03         (3)
#define RX_MB_ID04         (4)
#define RX_MB_ID05         (5)
#define RX_MB_ID06         (6)
#define RX_MB_ID07         (7)
#define RX_MB_ID08         (8)
#define RX_MB_ID09         (9)
#define RX_MB_ID10         (10)
#define RX_MB_ID11         (11)
#define RX_MB_ID12         (12)
#define RX_MB_ID13         (13)
#define RX_MB_ID14         (14)
#define RX_MB_ID15         (15)
#define RX_MB_ID16         (16)
#define RX_MB_ID17         (17)
#define RX_MB_ID18         (18)
#define RX_MB_ID19         (19)
#define RX_MB_ID20         (20)
#define RX_MB_ID21         (21)
#define RX_MB_ID22         (22)
#define RX_MB_ID23         (23)
#define RX_MB_ID24         (24)
#define RX_MB_ID25         (25)
#define RX_MB_ID26         (26)
#define RX_MB_ID27         (27)
#define RX_MB_ID28         (28)
#define RX_MB_ID29         (29)
#define RX_MB_ID30         (30)
#define RX_MB_ID31         (31)
#define RX_MB_IDM          (32)
/**
 * @brief 
 */
typedef struct st_can_frame
{
	uint8_t frame_type;    /* 帧类型 远程/数据 */
	uint8_t id_type;       /* ID类型 标准/扩展*/
	uint8_t dlc;      /* 数据长度 */
	uint32_t id;           /* 报文 ID*/
	union 
	{
		uint8_t byte_buf[8];
		uint16_t half_buf[4];
		uint32_t word_buf[2];
	}data;
}can_frame_type;

/**
 * @brief CAN接收函数指针
 */
typedef void(*drv_can_cb_pfunc)(uint8_t ,can_frame_type);

/* @brief */

/* Variables -----------------------------------------------------------------*/

/*******************************************************************************
 * public API List
 ******************************************************************************/
void drv_can_init(uint8_t can_id , uint32_t can_speed);
void drv_can_set_rx_mb_mask(uint8_t can_id , uint8_t rx_mb , 
                            uint8_t id_type , uint32_t rx_id ,uint32_t mask_id);
uint8_t drv_can_send_frame(uint8_t can_id , can_frame_type send_msg);
uint8_t drv_can_read_frame(uint8_t can_id ,uint8_t mailbox , can_frame_type *can_msg);
void drv_can_int_rx_func(uint8_t can_id , uint8_t mailbox);
void drv_can_set_rx_cb_func(uint8_t can_id , drv_can_cb_pfunc pfunc);
void drv_can_deinit(uint8_t can_id);
void drv_can_reset_init(uint8_t can_id , uint32_t can_speed);

#ifdef __cplusplus
}
#endif

#endif  /* #ifndef __DRV_CAN_H_ */
/*****************************(C)Copyright end *******************************/



